<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
class MY_fclass {
	public function __construct()
	{
	//	echo "Thịnh xai rồi";
		//exit();
	}

	public function clean($str){
		$str = @trim($str);
		if(get_magic_quotes_gpc()) $str = stripslashes($str);
		return mysql_real_escape_string($str);
	}

	public function cleanup_text($data)
	{
		$data= stripslashes($data);
		$data= mysql_real_escape_string($data);
		return trim($data);
	}

	public function check_permission($tab,$per_name,$bits,$permissions){
		$array_permissions = $this->convert_bits_to_array($bits[$tab], $permissions[$tab]);
		if(isset($array_permissions[$per_name]) && $array_permissions[$per_name])
			return 1;
		else
			return 0;
	}

	public function convert_bits_to_array($bitfield, $_FIELDNAMES){
		$bitfield = intval($bitfield);
		$arry = array();
		if(count($_FIELDNAMES)>0){
			foreach ($_FIELDNAMES AS $field => $bitvalue){
				if ($bitfield & $bitvalue)
					$arry["$field"] = 1;
				else
					$arry["$field"] = 0;
				}	
		}
		
		return $arry;
	}

	public function convert_array_to_bits($arry, $_FIELDNAMES, $unset = 0){
		$bits = 0;
		foreach($_FIELDNAMES AS $fieldname => $bitvalue){
			if (isset($arry[$fieldname]) && $arry[$fieldname])
				$bits += $bitvalue;
			if ($unset)
				unset($arry["$fieldname"]);
		}
		return $bits;
	}



	function subStringVN($value, $length)
	{
		if($value!=''){
			if(is_array($value)) list($string, $match_to) = $value;
			else { $string = $value; $match_to = $value{0}; }

			$match_start = stristr($string, $match_to);
			$match_compute = strlen($string) - strlen($match_start);

			if (strlen($string) > $length)
			{
				if ($match_compute < ($length - strlen($match_to)))
				{
					$pre_string = substr($string, 0, $length);
					$pos_end = strrpos($pre_string, " ");
					if($pos_end === false) $string = $pre_string."...";
					else $string = substr($pre_string, 0, $pos_end)."...";
				}
				else if ($match_compute > (strlen($string) - ($length - strlen($match_to))))
				{
					$pre_string = substr($string, (strlen($string) - ($length - strlen($match_to))));
					$pos_start = strpos($pre_string, " ");
					$string = "...".substr($pre_string, $pos_start);
					if($pos_start === false) $string = "...".$pre_string;
					else $string = "...".substr($pre_string, $pos_start);
				}
				else
				{
					$pre_string = substr($string, ($match_compute - round(($length / 3))), $length);
					$pos_start = strpos($pre_string, " "); $pos_end = strrpos($pre_string, " ");
					$string = "...".substr($pre_string, $pos_start, $pos_end)."...";
					if($pos_start === false && $pos_end === false) $string = "...".$pre_string."...";
					else $string = "...".substr($pre_string, $pos_start, $pos_end)."...";
				}

				$match_start = stristr($string, $match_to);
				$match_compute = strlen($string) - strlen($match_start);
			}

			return $string;
		}else{
			return $string ='';
		}
	}

	public function getContent($url){
		$ch = curl_init($url);		 
		if (! $ch) {
		 die( "Lỗi trong quá trình khởi tạo cURL" );
		}
		 
		 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

		return $data = curl_exec($ch);
		 
		// đóng kết nối
		curl_close($ch);
	}
}
